cmake_minimum_required(VERSION 3.5)
project(PplJitHost LANGUAGES C CXX)

option(BUILD_STATIC "Build as static library (default)" ON)
option(BUILD_DYN_HOST "Build as static library (default)" OFF)
set(BUILD_DIR "build" CACHE STRING "Build directory")

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wl,--no-undefined")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wl,--no-undefined")
set(NNTOOLCHAIN_DIRECTORY $ENV{PROJECT_ROOT}/third_party/nntoolchain)

if(NOT DEFINED ENV{PPL_PROJECT_ROOT})
  message(FATAL_ERROR "Please set environ PPL_PROJECT_ROOT to ppl release path")
else()
  set(PPL_TOP $ENV{PPL_PROJECT_ROOT})
  message(NOTICE "PPL_PATH: ${PPL_TOP}")
endif()

if(DEBUG)
  set(CMAKE_BUILD_TYPE "Debug")
  add_definitions(-DDEBUG)
else()
  set(CMAKE_BUILD_TYPE "Release")
  if(NOT USING_CUDA)
    add_definitions(-O3)
  endif()
endif()

set(KERNEL_TOP ${PPL_TOP}/deps/common/dev/kernel)
set(HOST_TOP ${PPL_TOP}/deps/common/host)

include_directories(${KERNEL_TOP})
include_directories(${HOST_TOP}/include)
include_directories($ENV{TPUC_ROOT}/include)
include_directories(include)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/${BUILD_DIR}/include)
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/${BUILD_DIR}/host PL_SRC)

# compile cpp
aux_source_directory(src TILING_SRC)
include($ENV{PPL_RUNTIME_PATH}/scripts/GenChipDef.cmake)
if(BUILD_STATIC)
  set(ADDITIONAL_SRC ${HOST_TOP}/src/ppl_jit.cpp
                    ${HOST_TOP}/src/host_utils.cpp)
  add_library(ppl_host SHARED ${TILING_SRC} ${PL_SRC} ${ADDITIONAL_SRC})

  install(TARGETS ppl_host DESTINATION lib)
  if(NOT DEBUG)
    install(FILES  $<TARGET_FILE:ppl_host> DESTINATION ${NNTOOLCHAIN_DIRECTORY}/ppl)
  endif()
else()
  if(DEFINED DEV_MODE)
    message(NOTICE "DEV_MODE: ${DEV_MODE}")
  endif()
  list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}")
  include(Dynkernel)
  if(BUILD_DYN_HOST)
    set(DYN_HOST_NAME ppl_dyn_host_${CHIP})
    set(ADDITIONAL_SRC ${HOST_TOP}/src/host_utils.cpp)
    add_library(${DYN_HOST_NAME} SHARED ${TILING_SRC} ${PL_SRC} ${ADDITIONAL_SRC})
    install(TARGETS ${DYN_HOST_NAME} DESTINATION lib)
    if(NOT DEBUG)
      install(FILES  $<TARGET_FILE:${DYN_HOST_NAME}> DESTINATION ${NNTOOLCHAIN_DIRECTORY}/ppl)
    endif()
  endif()
endif()
